home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / BOOKAUTO.AML < prev    next >
Text File  |  1996-07-17  |  4KB  |  151 lines

  1. //--------------------------------------------------------------------
  2. // BOOKAUTO.AML
  3. // Save/Restore Bookmarks Automatically, (C) 1993-1996 by nuText Systems
  4. //
  5. // (See Bookauto.dox for user help)
  6. //
  7. // Running this macro will automatically save bookmarks to history
  8. // whenever a file is closed, and restore bookmarks from history
  9. // whenever a file is opened.
  10. //
  11. // If the name of a bookmark to be restored is already in use, a new
  12. // bookmark name is assigned.
  13. //
  14. // Usage:
  15. //
  16. // Select this macro from the Macro List (on the Macro menu), or run it
  17. // from the macro picklist <shift f12>.
  18. //--------------------------------------------------------------------
  19.  
  20. include bootpath "define.aml"
  21.  
  22. // bookmark history buffer
  23. constant HISTORY = '_book'
  24.  
  25. // book field delimiters and regexp patterns
  26. constant DELIM = '\x00'
  27. constant FIELD = '{[~' + DELIM + ']+}' + DELIM + '#'
  28. constant BOOKFIELDS = FIELD + FIELD + FIELD + FIELD + FIELD + FIELD
  29.  
  30. // temporary bookmark name
  31. constant tempbook = '@T'
  32.  
  33. // check for a previous instance
  34. if object? prf.bookauto then
  35.   if (okbox "Bookauto already installed. Remove it?") == 'Ok' then
  36.     destroyobject prf.bookauto
  37.     prf.bookauto = ''
  38.   end
  39.   return
  40. end
  41. prf.bookauto = getcurrobj
  42.  
  43.  
  44. // edit windows only
  45. object edit
  46.  
  47. // called when closing edit windows
  48. function onclose
  49.  
  50.   // call previous onclose
  51.   passprev
  52.  
  53.   name = getbufname
  54.  
  55.   // delete old bookmarks from the history buffer for this file
  56.   oldbuf = gotobuf HISTORY
  57.   if oldbuf then
  58.     while find DELIM + name + DELIM do
  59.       delline
  60.       col 1
  61.     end
  62.     gotobuf oldbuf
  63.   end
  64.  
  65.   // check for bookmarks in the current buffer
  66.   bookmark = getcurrbook
  67.   if not bookmark then
  68.     return
  69.   end
  70.  
  71.   // save the current cursor position and window view in
  72.   // a temporary bookmark
  73.   setbook tempbook
  74.  
  75.   // cycle through bookmarks for this buffer
  76.   while bookmark do
  77.     if bookmark <> tempbook then
  78.       // move cursor to the bookmark --and change the window view
  79.       gotobook bookmark
  80.       // add bookmark data to history
  81.       addhistory HISTORY bookmark + DELIM + name + DELIM +
  82.                  getviewleft + DELIM + getviewtop + DELIM +
  83.                  getcol + DELIM + getrow + DELIM
  84.     end
  85.     bookmark = getprevbook bookmark
  86.   end
  87.  
  88.   // restore the cursor position and window view
  89.   // and destroy the temp bookmark
  90.   gotobook tempbook
  91.   destroybook tempbook
  92. end
  93.  
  94.  
  95. // called when opening edit windows
  96. function onopen (options)
  97.  
  98.   variable bookmark, filename, viewleft, viewtop, column, line
  99.  
  100.   // call previous onopen
  101.   passprev options
  102.  
  103.   name = getbufname
  104.   pushcursor
  105.  
  106.   // restore bookmarks from the bookmark history buffer
  107.   oldbuf = gotobuf HISTORY
  108.   if oldbuf then
  109.     pushcursor
  110.     gotopos 1 1
  111.     // find bookmarks for this filename
  112.     while find DELIM + name + DELIM do
  113.       // parse history line into fields
  114.       parse BOOKFIELDS (gettext) 'x' ref bookmark ref filename
  115.                                      ref viewleft ref viewtop
  116.                                      ref column   ref line
  117.       // set the bookmark
  118.       gotobuf oldbuf
  119.       scrollcol viewleft
  120.       scrollrow viewtop
  121.       gotopos column line
  122.       // bookmark name already used?
  123.       if book? bookmark then
  124.         // remove trailing numeric digits to get base name
  125.         bookbase = bookmark [1..posnot '0-9' bookmark 'r']
  126.         // find a unique name
  127.         i = 1
  128.         loop
  129.           bookmark = concat bookbase i
  130.           if not book? bookmark then
  131.             break
  132.           end
  133.           i = i + 1
  134.         end
  135.       end
  136.       // set the bookmark
  137.       setbook bookmark
  138.       gotobuf HISTORY
  139.     end
  140.     popcursor
  141.     gotobuf oldbuf
  142.   end
  143.   popcursor
  144. end
  145.  
  146. // keep this macro resident
  147. resident ON
  148.  
  149. display
  150. say "Bookauto installed"
  151.